        package com.ruoyi.web.controller.common;
import cn.hutool.core.codec.Base64Decoder;
import com.ruoyi.common.config.FaceConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.FaceUtil;
import com.ruoyi.system.domain.AttendanceRecord;
import com.ruoyi.system.domain.CustomVisit;
import com.ruoyi.system.service.FaceService;
import com.ruoyi.system.service.IAttendanceRecordService;
import com.ruoyi.system.service.ICustomVisitService;
import com.ruoyi.web.controller.entity.ImageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/common")
public class FaceController extends BaseController {

    @Autowired
    private FaceService faceService;

    @Autowired
    private IAttendanceRecordService attendanceRecordService;

    @Autowired
    private ICustomVisitService customVisitService;

    /**
     * 访客人脸识别进入
     * @return
     */
    @PostMapping("/visitEnter")
    @ResponseBody
    public AjaxResult enter(@RequestBody ImageInfo imageInfo) throws Exception {
        String imageUrl = imageInfo.getImageUrl();
        Long id = imageInfo.getId();
        byte[] currentUserFeature = getCurrentUserFeature();
        byte[] feature = getFeature(imageUrl);
        float faceComparison = faceService.faceComparison(currentUserFeature, feature);
        if (faceComparison >= FaceConfig.SIMILARITY_THRESHOLD) {
            CustomVisit customVisit = customVisitService.selectCustomVisitById(id);
            customVisit.setStatus("3");
            customVisit.setEnterTime(new Date());
            customVisitService.updateCustomVisit(customVisit);
            return AjaxResult.success("访客人脸识别成功, 相似度: " + faceComparison);
        } else {
            return AjaxResult.error("比对相似度较低，不是同一人");
        }
    }

    /**
     * 访客人脸识别离场
     */
    @PostMapping("/visitLeave")
    @ResponseBody
    public AjaxResult leave(@RequestBody ImageInfo imageInfo) throws Exception {
        String imageUrl = imageInfo.getImageUrl();
        Long id = imageInfo.getId();
        byte[] currentUserFeature = getCurrentUserFeature();
        byte[] feature = getFeature(imageUrl);
        float faceComparison = faceService.faceComparison(currentUserFeature, feature);
        if (faceComparison >= FaceConfig.SIMILARITY_THRESHOLD) {
            CustomVisit customVisit = customVisitService.selectCustomVisitById(id);
            customVisit.setStatus("4");
            customVisit.setLeaveTime(new Date());
            customVisitService.updateCustomVisit(customVisit);
            return AjaxResult.success("访客人脸识别成功, 相似度: " + faceComparison);
        } else {
            return AjaxResult.error("比对相似度较低，不是同一人");
        }
    }

    /**
     * 员工人脸识别进出场
     */
    @PostMapping("/employeeEnterOrLeave")
    @ResponseBody
    public AjaxResult employeeEnterOrLeave(@RequestBody ImageInfo imageInfo) throws Exception {
        String imageUrl = imageInfo.getImageUrl();
        String type = imageInfo.getType();
        byte[] currentUserFeature = getCurrentUserFeature();
        byte[] feature = getFeature(imageUrl);
        float faceComparison = faceService.faceComparison(currentUserFeature, feature);
        if (faceComparison >= FaceConfig.SIMILARITY_THRESHOLD) {
            AttendanceRecord attendanceRecord = new AttendanceRecord();
            attendanceRecord.setType(type);
            attendanceRecord.setUserId(getUserId());
            attendanceRecord.setCreateTime(new Date());
            attendanceRecordService.insertAttendanceRecord(attendanceRecord);
            return AjaxResult.success("比对人脸识别成功, 相似度: " + faceComparison);
        } else {
            return AjaxResult.error("识别相似度较低，不是同一人");
        }
    }

    public byte[] getCurrentUserFeature() {
        String feature = getLoginUser().getUser().getFeature();
        return Base64Decoder.decode(feature);
    }

    public byte[] getFeature(String imagePath) {
        byte[] imageByte = FaceUtil.getImageByte(imagePath);
        return faceService.faceFeature(imageByte);
    }

}